package com.unytech.wisdomcommunity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.unytech.wisdomcommunity.R;
import com.unytech.wisdomcommunity.constant.SPConstants;
import com.unytech.wisdomcommunity.utils.SPUtils;

/**
 * Created by zed on 2016/10/27/0027.
 */

/**
 * 欢迎界面
 */
public class WelcomeActivity extends AppCompatActivity {

    @ViewInject(R.id.welcome_iv)
    ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ViewUtils.inject(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        playAnimation();
    }

    @Override
    protected void onStop() {
        super.onStop();
        sHandler.removeCallbacks(mGoNextActivityRunnable);
    }

    private void playAnimation() {

        AnimationSet animationSet = new AnimationSet(false);
        animationSet.setAnimationListener(mAnimationListener);

        ScaleAnimation scaleAnimation
                = new ScaleAnimation(
                0, 1, 0, 1,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
        );
        scaleAnimation.setDuration(1000);
        animationSet.addAnimation(scaleAnimation);

        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
        alphaAnimation.setDuration(1000);
        animationSet.addAnimation(alphaAnimation);

        mImageView.startAnimation(animationSet);
    }

    static Handler sHandler = new Handler();

    private Animation.AnimationListener mAnimationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }
        /**
         * 执行的动画,
         * @param animation
         */
        @Override
        public void onAnimationEnd(Animation animation) {
            sHandler.postDelayed(mGoNextActivityRunnable, 1000);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    };
    Runnable mGoNextActivityRunnable = new Runnable() {
        @Override
        public void run() {
            goNextActivity();
        }
    };

    private void goNextActivity() {
        if(SPUtils.getBoolean(WelcomeActivity.this, SPConstants.IS_COMPLETED)){
            startActivity(new Intent(this,Main2Activity.class));
            finish();
        }else {
            startActivity(new Intent(this,GuideActivity.class));
            SPUtils.put(this, SPConstants.IS_COMPLETED, true);
            finish();
        }
    }
}